<!--
 * @Date: 2025-01-16 11:03:40
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2025-01-20 11:05:48
 * @Description: 主页面板
-->
<template>
  <LayoutFull :="layoutAttrs" >
    <template #aside>
      <ClassifyTree :="treeAttrs" @currentChange="treeNodeClick" />
    </template>
    <template #main>
      <div class="home-panel">
        <div class="home-panel-tools">
          <div class="tools-left">
            <el-input
              v-model="homeParams.keyWords"
              clearable
              placeholder="请输入名称/编码"
              @change="loadHomeData"
            >
            </el-input>
          </div>
          <div class="tools-right">
            <div class="mode-bar">
              <div
                v-for="item in modeList"
                :key="item.type"
                :class="[
                  'mode-bar-item',
                  homeState.mode === item.type ? 'active' : ''
                ]"
                @click="modeChange(item)"
              >
                <el-icon><component :is="item.icon" /></el-icon>
              </div>
            </div>
          </div>
        </div>
        <div class="home-panel-content" v-loading="homeState.loading">
          <div class="home-mode-panel" v-if="homeState.mode === 'panel'">
            <el-scrollbar>
              <div class="home-cards" v-if="homeState.homeList.length">
                <el-card
                  v-for="item in homeState.homeList"
                  :key="item.formId"
                  :class="[
                    'home-card-item',
                    homeState.checkItem && homeState.checkItem.formId === item.formId ? 'active' : ''
                  ]"
                  shadow="hover"
                >
                  <div class="item-body" @click="handleCheckedItem(item)">
                    <img class="item-image" :src="item.imageStr" />
                    <div class="item-checked" v-if="homeState.checkItem && homeState.checkItem.formId === item.formId">
                      <RkIcon color="#409eff" name="ele-CircleCheckFilled" size="28px"/>
                    </div>
                  </div>
                  <template #footer>
                    <div class="item-footer">
                      <div class="footer-left">
                        <span :title="item.name">{{ item.name }}</span>
                      </div>
                      <div class="footer-right">
                        <el-tooltip placement="top" content="预览">
                          <RkIcon name="ele-View" size="16px" @click="handlePreview(item)"/>
                        </el-tooltip>
                      </div>
                    </div>
                  </template>
                </el-card>
              </div>
              <div class="home-empty" v-else >
                <el-empty description="暂无数据" />
              </div>
            </el-scrollbar>
          </div>
          <div class="home-mode-table" v-if="homeState.mode === 'table'">
            <rk-grid
              ref="tableRef"
              :="tableAttrs"
              :data="homeState.homeList"
              :loading='homeState.loading'
              @radioChange="tableRadioChange"
            >
              <template #operation="{ row }">
                <el-button link type="primary" size="small" @click.stop="handlePreview(row)">预览</el-button>
              </template>
            </rk-grid>
          </div>
        </div>
      </div>
    </template>
    <HpPreviewDrawer ref="previewRef"/>
  </LayoutFull>
  
</template>

<script setup>
import { defAxios } from '@/utils/http'
import { nextTick, reactive, ref } from 'vue';
import { List, Grid } from '@element-plus/icons-vue'
import { HpPreviewDrawer } from '@hp/drawer'
import RkIcon from '@/components/rk-icon/src/RkIcon.vue';

// 布局属性配置
const layoutAttrs = reactive({
  enableAside: true,
  enableHeader: false,
})

// 树结构属性
const treeAttrs = reactive({
  // 标题
  title: '主页分类',
  // 类型区分
  funcType: '3',
  // 开启设置 - 不开启
  enableSet: false,
})
// 树结构数据
const treeState = reactive({
  selectNode: {},
  selectData: {},
})
/**
 * 树结构点击事件
 * 
 * @param data 树节点数据
 */
const treeNodeClick = (data) => {
  treeState.selectData = data
  homeParams.formType = data.code;
  homeState.checkItem = null
  loadHomeData()
}

// 预览组件的Ref
const previewRef = ref()
// 主页数据集
const homeState = reactive({
  // 模式：分为 table-表格  panel-面板
  mode: 'panel',
  loading: true,
  homeList: [],
  checkItem: null,
})
// 主页查询参数
const homeParams = reactive({
  status: '1',
  formType: undefined,
  keyWords: undefined
})
const modeList = [
  { type: 'panel', name: '面板模式', icon: Grid },
  { type: 'table', name: '表格模式', icon: List }
]
/**
 * 模式改变时执行的方法
 */
const modeChange = (item) => {
  if (item.type === homeState.mode) {
    return
  }
  homeState.mode = item.type
  nextTick(() => {
    if (homeState.checkItem && tableRef.value) {
      tableRef.value.setRadioRow(homeState.checkItem)
    }
  })
}
/**
 * 加载主页列表数据
 */
const loadHomeData = async () => {
  homeState.homeList = []
  homeState.loading = true
  const list = await reqGetHomeList(homeParams)
  homeState.homeList = list
  homeState.loading = false
}
/**
 * 预览
 * 
 * @param item 所选的数据
 */
const handlePreview = (item) => {
  previewRef.value.handleOpen(item)
}
/**
 * 选中一条主页数据
 * 
 * @param item 所选的数据
 */
const handleCheckedItem = (item) => {
  const { checkItem } = homeState
  if (checkItem && checkItem.formId === item.formId ) {
    clearCheckedData()
  } else {
    homeState.checkItem = item
  }
}

// 表格的Ref对象
const tableRef = ref()
// 表格属性配置
const tableAttrs = reactive({
  autoLoad: false,
  keyField: 'formId',
  boxConfig: { mode: 'radio' },
  pagerConfig: false,
  toolbarConfig: false,
  columns: [
    { field: 'name', title: '名称', align: 'left', headerAlign: 'center' },
    { field: 'code', title: '编码', align: 'left', headerAlign: 'center' },
    { field: 'operation', title: '操作', width: 80, align: 'center', slots: { default: 'operation' } },
  ]
})
/**
 * 表格单选选中
 * 
 * @param param 参数
 */
const tableRadioChange = (param) => {
  if (param) {
    homeState.checkItem = JSON.parse(JSON.stringify(param.row))
  } else {
    homeState.checkItem = null
  }
}

/**
 * 获取选中的行数据
 */
const getCheckedData = () => {
  if (tableRef.value) {
    return JSON.parse(JSON.stringify(tableRef.value.getRadioRecord()))
  } else {
    return homeState.checkItem
  }
}
/**
 * 清除选中的行数据
 */
const clearCheckedData = () => {
  homeState.checkItem = null
  if (tableRef.value) {
    tableRef.value.clearRadioRow()
  }
}

/**
 * 请求获取首页列表数据
 * 
 * @param params 参数
 */
const reqGetHomeList = (params) => {
  return new Promise(resolve => {
    defAxios({
      url: '/prdFormInfo/queryPrdFormInfoList',
      data: params,
      method: 'POST',
    }).then(res => {
      if (res.code === 200) {
        resolve(res.data)
      } else {
        console.error(res.msg)
        resolve([])
      }
    }).catch(err => {
      console.error(err)
      resolve([])
    })
  })
}

defineExpose({
  getCheckedData,
  clearCheckedData
})

</script>

<style lang="scss" scoped>
.home-panel {
  width: 100%;
  height: 100%;
  // padding: 10px;
  position: relative;
  display: flex;
  flex-direction: column;
  .home-panel-tools {
    display: flex;
    padding: 0 10px;
    height: 50px;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #e8e8e8;
    .mode-bar {
      display: flex;
      .mode-bar-item {
        display: flex;
        align-items: center;
        color: #636363;
        border: 1px solid #bdbdbd;
        padding: 4px 10px;
        border-radius: 4px;
        cursor: pointer;
        &.active {
          color: #fff;
          border-color: #409eff;
          background-color: #409eff;
        }
        &:first-child {
          border-top-right-radius: 0;
          border-bottom-right-radius: 0;
          border-right-color: rgb(255 255 255 / 50%);
        }
        &:last-child {
          border-top-left-radius: 0;
          border-bottom-left-radius: 0;
        }
        &:not(:last-child) {
          margin-right: -1px;
        }
      }
    }
  }
  .home-panel-content {
    flex: 1;
    min-height: 20px;
    .home-mode-panel,
    .home-mode-table {
      height: 100%;
    }
    .home-mode-table {
      padding: 10px;
    }
  }
  .home-cards {
    display: grid;
    gap: 12px 12px;
    padding: 10px;
    grid-template-columns: repeat(3, minmax(50px,1fr));
    .home-card-item {
      display: flex;
      flex-direction: column;
      height: 200px;
      border-radius: 4px;
      cursor: pointer;
      &.active {
        border: 2px solid #409eff;
      }
      :deep(.el-card__body) {
        padding: 5px;
        flex: 1;
        min-height: 20px;
        position: relative;
      }
      :deep(.el-card__footer) {
        padding: 0;
      }
      .item-body {
        height: 100%;
        border: 1px solid var(--el-card-border-color);
        border-radius: 3px;
      }
      .item-image {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
      .item-checked {
        position: absolute;
        top: 0;
        right: 0;
        left: 0;
        bottom: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
        background: rgba(99, 99, 99, 0.316);
      }
      .item-footer {
        font-size: 14px;
        width: 100%;
        height: 36px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 8px;
        .footer-left {
          font-weight: 500;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .footer-right {
          margin-left: 12px;
          display: flex;
          align-items: center;
        }
      }
    }
  }
  :deep(.vxe-table--border-line) {
    border: var(--vxe-table-border-width) solid var(--vxe-table-border-color) !important;
  }
}
</style>